/*
 * 权限组件
 * @Author: copy from antd pro
 * @Date: 2020-05-30 06:30:33
 * @Last Modified by: ahwgs
 * @Last Modified time: 2020-06-01 06:49:43
 */

import React from 'react';
import ErrorBoundary from '@/components/ErrorBoundary';
import { useAuth } from '@/hooks';
import { Redirect } from 'umi';

interface AuthComponentProps {
  // user: IUserModelState;
}

const AuthComponent: React.FC<any> = ({
  children,
  route = {
    routes: [],
  },
  location = {
    pathname: '',
  },
}) => {
  const { isLogin } = useAuth(route);
  const redirect = window.encodeURIComponent(`${location.pathname}${location.search}`);
  return (
    <ErrorBoundary>
      {isLogin ? children : <Redirect to={`/login?redirect=${redirect}`} />}
    </ErrorBoundary>
  );
};

export default AuthComponent;
